//
//  ShowListView.swift
//  testc
//
//  Created by Ares Cheuk on 2024/3/30.
//

import SwiftUI

struct ShowListView: View {
    let notificationName = "CcNotification"

    let sectionProducts = [
        productModel(type: "家具",
                     productInfos: [
                        productInfoModel(productName: "大陈陈1", productImageName: "slide_01_metal",productEntityName:"Sun"),
                        productInfoModel(productName: "大陈陈2", productImageName: "slide_01_metal",productEntityName:"Sun"),
                        productInfoModel(productName: "大陈陈3", productImageName: "slide_01_metal",productEntityName:"Sun"),
                        productInfoModel(productName: "大陈陈4", productImageName: "slide_01_metal",productEntityName:"Sun"),
                        productInfoModel(productName: "大陈陈", productImageName: "slide_01_metal",productEntityName:"Sun")
                     ]),
        productModel(type: "电器",
                     productInfos: [
                        productInfoModel(productName: "电器大陈陈", productImageName: "slide_01_metal",productEntityName:"chairs"),
                        productInfoModel(productName: "电器大陈陈", productImageName: "slide_01_metal",productEntityName:"chairs"),
                        productInfoModel(productName: "电器大陈陈", productImageName: "slide_01_metal",productEntityName:"chairs"),
                        productInfoModel(productName: "电器大陈陈", productImageName: "slide_01_metal",productEntityName:"chairs"),
                        productInfoModel(productName: "电器大陈陈", productImageName: "slide_01_metal",productEntityName:"chairs"),
                     ]),
    ]
    
    var body: some View {
        
        VStack{
            NavigationStack {
                VStack{
                    
                    List {
                        ForEach(sectionProducts) { section in
                            Section(content: {
                                ForEach(section.productInfos) { item in
                                    
                                    HStack{
                                        Text(item.productName)
                                        Image(item.productImageName)
                                            .resizable()
                                            .scaledToFit()
                                            .opacity(0.5)
                                            .frame(width: 100,height: 40)
                                            .padding(.leading , 50)
                                        
                                        Button {
                                            NSLog("%@", item.productName)
                                            
                                            NotificationCenter.default.post(name: NSNotification.Name(rawValue:notificationName), object: item.productEntityName, userInfo: ["EntityName" : item.productEntityName])
                                        } label: {
                                        }
                                    }
                                }
                            }, header: {
                                Text("分类 \(section.type)")
                            })
                        }
                    }.navigationTitle("分类列表")
                }
            }
        }
    }
}



// MARK: - Model
struct productInfoModel: Identifiable {
    let productName: String
    let productImageName: String
    let productEntityName: String

    let id = UUID()
}

struct productModel: Identifiable {
    let id = UUID()
    let type: String
    let productInfos: [productInfoModel]
}

// MARK: - Preview

#Preview {
    ShowListView()
}
